package com.lineage.server.datatables;

import com.lineage.DatabaseFactory;
import com.lineage.server.templates.MapData;
import com.lineage.server.utils.PerformanceTimer;
import com.lineage.server.utils.SQLUtil;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public final class MapsTable {
  private static final Log _log = LogFactory.getLog(MapsTable.class);
  
  private static final Map<Integer, MapData> _maps = new HashMap<>();
  
  private static final Map<Object, Object> _mapName = new HashMap<>();
  
  private static MapsTable _instance;
  
  public static MapsTable get() {
    if (_instance == null)
      _instance = new MapsTable(); 
    return _instance;
  }
  
  public void load() {
    PerformanceTimer timer = new PerformanceTimer();
    Connection con = null;
    PreparedStatement pstm = null;
    ResultSet rs = null;
    try {
      con = DatabaseFactory.get().getConnection();
      pstm = con.prepareStatement("SELECT * FROM `mapids`");
      rs = pstm.executeQuery();
      while (rs.next()) {
        MapData data = new MapData();
        int mapId = rs.getInt("mapid");
        String mapName = rs.getString("locationname");
        _mapName.put(Integer.valueOf(mapId), mapName);
        data.mapId = mapId;
        data.startX = rs.getInt("startX");
        data.endX = rs.getInt("endX");
        data.startY = rs.getInt("startY");
        data.endY = rs.getInt("endY");
        data.monster_amount = rs.getDouble("monster_amount");
        data.dropRate = rs.getDouble("drop_rate");
        data.isUnderwater = rs.getBoolean("underwater");
        data.markable = rs.getBoolean("markable");
        data.teleportable = rs.getBoolean("teleportable");
        data.escapable = rs.getBoolean("escapable");
        data.isUseResurrection = rs.getBoolean("resurrection");
        data.isUsePainwand = rs.getBoolean("painwand");
        data.isEnabledDeathPenalty = rs.getBoolean("penalty");
        data.isTakePets = rs.getBoolean("take_pets");
        data.isRecallPets = rs.getBoolean("recall_pets");
        data.isUsableItem = rs.getBoolean("usable_item");
        data.isUsableSkill = rs.getBoolean("usable_skill");
        data.isGuaji = rs.getBoolean("isguaji");
        data.isClanPc = rs.getBoolean("isClanPc");
        data.isPartyPc = rs.getBoolean("isPartyPc");
        data.isAlliancePc = rs.getBoolean("isAlliancePc");
        data.isdropitem = rs.getBoolean("isdropitem");
        data.isUsableShop = rs.getInt("usable_shop");
        data.isStart_time = rs.getInt("Start_time");
        data.isEnd_time = rs.getInt("End_time");
        data.isweek = rs.getInt("week");
        data.CopyMapId = rs.getInt("CopyMapId");
        data.EXP = rs.getInt("EXP");
        _maps.put(new Integer(mapId), data);
      } 
    } catch (SQLException e) {
      _log.error(e.getLocalizedMessage(), e);
    } finally {
      SQLUtil.close(rs);
      SQLUtil.close(pstm);
      SQLUtil.close(con);
    } 
    _log.info("载入地图设置资料数量: " + _maps.size() + "(" + timer.get() + "ms)");
  }
  
  public int isUsableShop(int mapId) {
    MapData map = _maps.get(Integer.valueOf(mapId));
    if (map == null)
      return 0; 
    return ((MapData)_maps.get(Integer.valueOf(mapId))).isUsableShop;
  }
  
  public int isStart_time(int mapId) {
    MapData map = _maps.get(Integer.valueOf(mapId));
    if (map == null)
      return 0; 
    return ((MapData)_maps.get(Integer.valueOf(mapId))).isStart_time;
  }
  
  public int isEnd_time(int mapId) {
    MapData map = _maps.get(Integer.valueOf(mapId));
    if (map == null)
      return 0; 
    return ((MapData)_maps.get(Integer.valueOf(mapId))).isEnd_time;
  }
  
  public int isweek(int mapId) {
    MapData map = _maps.get(Integer.valueOf(mapId));
    if (map == null)
      return 0; 
    return ((MapData)_maps.get(Integer.valueOf(mapId))).isweek;
  }
  
  public int getMapTime(int mapId) {
    MapData map = _maps.get(Integer.valueOf(mapId));
    if (map == null)
      return 0; 
    return ((MapData)_maps.get(Integer.valueOf(mapId))).maptime;
  }
  
  public boolean isTimingMap(int mapId) {
    MapData map = _maps.get(Integer.valueOf(mapId));
    return (map != null && map.maptime > 0);
  }
  
  public Map<Integer, MapData> getMaps() {
    return _maps;
  }
  
  public void setMaps(int mapId, MapData mapdata) {
    _maps.put(Integer.valueOf(mapId), mapdata);
  }
  
  public MapData getMapData(int mapId) {
    MapData map = _maps.get(Integer.valueOf(mapId));
    return map;
  }
  
  public int getStartX(int mapId) {
    MapData map = _maps.get(Integer.valueOf(mapId));
    if (map == null)
      return 0; 
    return ((MapData)_maps.get(Integer.valueOf(mapId))).startX;
  }
  
  public int getEndX(int mapId) {
    MapData map = _maps.get(Integer.valueOf(mapId));
    if (map == null)
      return 0; 
    return ((MapData)_maps.get(Integer.valueOf(mapId))).endX;
  }
  
  public int getStartY(int mapId) {
    MapData map = _maps.get(Integer.valueOf(mapId));
    if (map == null)
      return 0; 
    return ((MapData)_maps.get(Integer.valueOf(mapId))).startY;
  }
  
  public int getEndY(int mapId) {
    MapData map = _maps.get(Integer.valueOf(mapId));
    if (map == null)
      return 0; 
    return ((MapData)_maps.get(Integer.valueOf(mapId))).endY;
  }
  
  public double getMonsterAmount(int mapId) {
    MapData map = _maps.get(Integer.valueOf(mapId));
    if (map == null)
      return 0.0D; 
    return map.monster_amount;
  }
  
  public double getDropRate(int mapId) {
    MapData map = _maps.get(Integer.valueOf(mapId));
    if (map == null)
      return 0.0D; 
    return map.dropRate;
  }
  
  public boolean isUnderwater(int mapId) {
    MapData map = _maps.get(Integer.valueOf(mapId));
    return (map != null && ((MapData)_maps.get(Integer.valueOf(mapId))).isUnderwater);
  }
  
  public boolean isMarkable(int mapId) {
    MapData map = _maps.get(Integer.valueOf(mapId));
    return (map != null && ((MapData)_maps.get(Integer.valueOf(mapId))).markable);
  }
  
  public boolean isTeleportable(int mapId) {
    MapData map = _maps.get(Integer.valueOf(mapId));
    return (map != null && ((MapData)_maps.get(Integer.valueOf(mapId))).teleportable);
  }
  
  public boolean isEscapable(int mapId) {
    MapData map = _maps.get(Integer.valueOf(mapId));
    return (map != null && ((MapData)_maps.get(Integer.valueOf(mapId))).escapable);
  }
  
  public boolean isUseResurrection(int mapId) {
    MapData map = _maps.get(Integer.valueOf(mapId));
    return (map != null && ((MapData)_maps.get(Integer.valueOf(mapId))).isUseResurrection);
  }
  
  public boolean isUsePainwand(int mapId) {
    MapData map = _maps.get(Integer.valueOf(mapId));
    return (map != null && ((MapData)_maps.get(Integer.valueOf(mapId))).isUsePainwand);
  }
  
  public boolean isEnabledDeathPenalty(int mapId) {
    MapData map = _maps.get(Integer.valueOf(mapId));
    return (map != null && ((MapData)_maps.get(Integer.valueOf(mapId))).isEnabledDeathPenalty);
  }
  
  public boolean isTakePets(int mapId) {
    MapData map = _maps.get(Integer.valueOf(mapId));
    return (map != null && ((MapData)_maps.get(Integer.valueOf(mapId))).isTakePets);
  }
  
  public boolean isRecallPets(int mapId) {
    MapData map = _maps.get(Integer.valueOf(mapId));
    return (map != null && ((MapData)_maps.get(Integer.valueOf(mapId))).isRecallPets);
  }
  
  public boolean isUsableItem(int mapId) {
    MapData map = _maps.get(Integer.valueOf(mapId));
    return (map != null && ((MapData)_maps.get(Integer.valueOf(mapId))).isUsableItem);
  }
  
  public boolean isUsableSkill(int mapId) {
    MapData map = _maps.get(Integer.valueOf(mapId));
    return (map != null && ((MapData)_maps.get(Integer.valueOf(mapId))).isUsableSkill);
  }
  
  public boolean isGuaji(int mapId) {
    MapData map = _maps.get(Integer.valueOf(mapId));
    return (map != null && ((MapData)_maps.get(Integer.valueOf(mapId))).isGuaji);
  }
  
  public boolean isClanPc(int mapId) {
    MapData map = _maps.get(Integer.valueOf(mapId));
    return (map != null && ((MapData)_maps.get(Integer.valueOf(mapId))).isClanPc);
  }
  
  public boolean isPartyPc(int mapId) {
    MapData map = _maps.get(Integer.valueOf(mapId));
    return (map != null && ((MapData)_maps.get(Integer.valueOf(mapId))).isPartyPc);
  }
  
  public boolean isAlliancePc(int mapId) {
    MapData map = _maps.get(Integer.valueOf(mapId));
    return (map != null && ((MapData)_maps.get(Integer.valueOf(mapId))).isAlliancePc);
  }
  
  public boolean isdropitem(int mapId) {
    MapData map = _maps.get(Integer.valueOf(mapId));
    return (map != null && ((MapData)_maps.get(Integer.valueOf(mapId))).isdropitem);
  }
  
  public String getLoaction(int mapId) {
    MapData map = _maps.get(Integer.valueOf(mapId));
    if (map == null)
      return ""; 
    return ((MapData)_maps.get(Integer.valueOf(mapId))).location;
  }
  
  public int getCopyMapId(int mapId) {
    MapData map = _maps.get(Integer.valueOf(mapId));
    if (map == null)
      return -1; 
    return ((MapData)_maps.get(Integer.valueOf(mapId))).CopyMapId;
  }
  
  public int getEXP(int mapId) {
    MapData map = _maps.get(Integer.valueOf(mapId));
    if (map == null)
      return 0; 
    return ((MapData)_maps.get(Integer.valueOf(mapId))).EXP;
  }
  
  public String getMapName(int mapId) {
    if (_mapName.containsKey(Integer.valueOf(mapId)))
      return (String)_mapName.get(Integer.valueOf(mapId)); 
    return null;
  }
}
